package com.milo.elastic.controller;

import com.milo.elastic.entity.Commodity;
import com.milo.elastic.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author milogenius
 * @date 2020-05-20 11:22
 */
@Slf4j
@Controller
@RequestMapping("elastic")
public class ElasticController {

    @Autowired
    private CommodityService commodityService;

    /**
     * 创建数据
     */
    @PostMapping("create")
    public void create( @RequestBody  Commodity commodity){
        Commodity save = commodityService.save(commodity);
        log.info("Commodity:{}",save.toString());
    }

    /**
     * 获取商品信息
     */
    @RequestMapping("get")
    public void get(){
        Iterable<Commodity> all = commodityService.getAll();
        all.forEach(item->{
            log.info(item.getName());
        });
    }

    /**
     * 搜索商品
     */
    @RequestMapping("search")
    public void search (@RequestParam String searchKey){
        //获取结果
        List<Commodity> byName = commodityService.getByName(searchKey);

    }
}
